/**
 * ======================================================================
 * 【基本说明】：
 * JavaScript实现的类似于Java等语言里的hashmap，用于方便存取key、value形式的数据。
 *
 * Created by Jack Jiang on 16-8-23.
 * ======================================================================
 */

function HashMap(){
    this.length = 0;
    this.maxLength = Number.MAX_VALUE;
    this.container = {};
}

HashMap.prototype.put = function(objName,objValue){
    try{
        if(this.length >= this.maxLength)
            throw new Error("[Error HashMap] : Map Datas' count overflow !");
        if(objName != ""){
            for(var p in this.container){
                if(p == objName){
                    this.container[objName] = objValue;
                    return ;
                }
            }
            this.container[objName] = objValue;
            this.length ++ ;
        }
    }catch(e){
        return e;
    }
};

HashMap.prototype.get = function(objName){
    try{
        if(this.container[objName])
            return this.container[objName];
    }catch(e){
        return e;
    }
};

HashMap.prototype.contains = function(objName){
    try{
        for(var p in this.container){
            if(p === objName)
                return true;
        }
        return false;
    }catch(e){
        return e;
    }
};

HashMap.prototype.containsValue = function(objValue){
    try{
        for(var p in this.container){
            if(this.container[p] === objValue)
                return true;
        }
        return false;
    }catch(e){
        return e;
    }
};

HashMap.prototype.remove = function(objName){
    try{
        if(this.container[objName]){
            delete this.container[objName];
            this.length -- ;
            return true;
        }
        return false;
    }
    catch(e){
        return e;
    }
};

//HashMap.prototype.pop = function(objName){
//    try{
//        var ov = this.container[objName];
//        if(ov){
//            delete this.container[objName];
//            this.length -- ;
//            return ov;
//        }
//        return null;
//    }catch(e){
//        return e;
//    }
//};

HashMap.prototype.removeAll = function(){
    try{
        this.clear();
    }catch(e){
        return e;
    }
};

HashMap.prototype.clear = function(){
    try{
        delete this.container;
        this.container = {};
        this.length = 0;
    }catch(e){
        return e;
    }
};

HashMap.prototype.isEmpty = function(){
    if(this.length === 0)
        return true;
    else
        return false;
};

//HashMap.prototype.runIn = function(fun){
//    try{
//        if(!fun)
//            throw new Error("[Error HashMap] : The paramer is null !");
//        for(var p in this.container){
//            var ov = this.container[p];
//            fun(ov);
//        }
//    }catch(e){
//        return e;
//    }
//};

// 本方法仅用于debug时
HashMap.prototype.showAll = function(funValueToString){
    if(this.length > 0)
    {
        console.log("[hashmap.js_showAll()] 正在输出HashMap内容(列表长度 %d) ------------------->"
            , this.length);
        //遍历
        for (var key in this.container) {
            if(funValueToString){
                console.log("[hashmap.js_showAll()]       > key=%s, value=%s", key, funValueToString(this.container[key]));
            }
            else{
                console.log("[hashmap.js_showAll()]       > key=%s, value=%s", key, this.container[key]
                );
            }
        }
    }
    else
    {
        console.log("[hashmap.js_showAll()] 列表中长度为：%d !", this.length);
    }
};


module.exports = HashMap;